import 'package:freedom/page/mahjong/common/message_content.dart';
import 'package:freedom/page/mahjong/message/abstract_action_message.dart';
import 'package:freedom/page/mahjong/message/abstract_message.dart';
import 'package:freedom/page/mahjong/message/action/bar_action.dart';
import 'package:freedom/page/mahjong/message/action/bump_action.dart';
import 'package:freedom/page/mahjong/message/action/call_cation.dart';
import 'package:freedom/page/mahjong/message/action/dice_action.dart';
import 'package:freedom/page/mahjong/message/action/exchange_action.dart';
import 'package:freedom/page/mahjong/message/action/export_card.dart';
import 'package:freedom/page/mahjong/message/action/hu_action.dart';
import 'package:freedom/page/mahjong/message/action/next_game_action.dart';
import 'package:freedom/page/mahjong/message/action/offline_action.dart';
import 'package:freedom/page/mahjong/message/action/online_action.dart';
import 'package:freedom/page/mahjong/message/action/prepare_action.dart';
import 'package:freedom/page/mahjong/message/action/refresh_action.dart';
import 'package:freedom/page/mahjong/message/action/refresh_room.dart';
import 'package:freedom/page/mahjong/message/action/snatch_bar_action.dart';
import 'package:freedom/page/mahjong/message/action/start_game_action.dart';
import 'package:freedom/page/mahjong/message/action/touch_card.dart';

class ActionMessageHandler extends AbstractMessageHandler {
  static const String PREPARE = "PREPARE";
  static const String EXCHANGE = "EXCHANGE";
  static const String EXPORT = "EXPORT";
  static const String BUMP = "BUMP";
  static const String REFRESH_ACTION = "REFRESH_ACTION";
  static const String BAR = "BAR";
  static const String TOUCH = "TOUCH";
  static const String CALL = "CALL";
  static const String HU = "HU";
  static const String PASS = "PASS";
  static const String REFRESH_ROOM = "REFRESH";
  static const String DICE = "DICE";

  static const String ONLINE = "ONLINE";
  static const String OFFLINE = "OFFLINE";
  static const String NEXT_GAMING = "NEXT_GAMING";
  static const String STARTING_GAME = "STARTING_GAME";
  static const String SNATCH_BAR = "SNATCH_BAR";
  Map<String, AbstractMessageActionHandler<MahjongMessage>> actionStrategy = {};

  ActionMessageHandler() {
    actionStrategy[EXPORT] = ExportCard();
    actionStrategy[TOUCH] = TouchCard();
    actionStrategy[REFRESH_ACTION] = RefreshAction();
    actionStrategy[REFRESH_ROOM] = RefreshRoom();
    actionStrategy[BUMP] = BumpAction();
    actionStrategy[BAR] = BarAction();
    actionStrategy[HU] = HuAction();
    actionStrategy[EXCHANGE] = ExchangeAction();
    actionStrategy[DICE] = DiceAction();
    actionStrategy[ONLINE] = OnlineAction();
    actionStrategy[OFFLINE] = OfflineAction();
    actionStrategy[CALL] = CallAction();
    actionStrategy[NEXT_GAMING] = NextGameingAction();
    actionStrategy[PREPARE] = PrepareAction();
    actionStrategy[STARTING_GAME] = StartGameAction();
    actionStrategy[SNATCH_BAR] = SnatchBarAction();
  }

  /// 这里action是数组  为了同时能下发多个事件执行
  /// 比如碰牌了 要播放动画 也要刷新房间
  /// AbstractMessageActionHandler
  /// 有两个参数  一个是父亲  body      一个是事件body

  @override
  void doMessage(MessageContent message) {
    List<MahjongMessage> actionArray = [];
    if (message.bodyContent is List) {
      message.bodyContent.forEach((element) {
        actionArray.add(MahjongMessage.fromJson(element));
      });
    } else if (message.bodyContent is Map) {
      actionArray.add(MahjongMessage.fromJson(message.bodyContent));
    }
    for (var action in actionArray) {
      if (!actionStrategy.containsKey(action.type)) {
        continue;
      }
      actionStrategy[action.type]?.doMessage(action);
    }
  }

  void doAction(List<MahjongMessage> actionArray) {
    for (var action in actionArray) {
      if (!actionStrategy.containsKey(action.type)) {
        continue;
      }
      actionStrategy[action.type]?.doMessage(action);
    }
  }
}
